/*
 *	Copyright 2011 ja4cli
 *
 *	Licensed under the Apache License, Version 2.0 (the "License");
 *	you may not use this file except in compliance with the License.
 *	You may obtain a copy of the License at
 *
 *		http://www.apache.org/licenses/LICENSE-2.0
 *
 *	Unless required by applicable law or agreed to in writing, software
 *	distributed under the License is distributed on an "AS IS" BASIS,
 *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *	See the License for the specific language governing permissions and
 *	limitations under the License.
 */
package org.skarby.ja4cli;

import java.util.List;
import java.util.Set;

import org.skarby.ja4cli.conversion.ArgumentConverter;
import org.skarby.ja4cli.conversion.ConverterFactory;
import org.skarby.ja4cli.internal.ArgumentSegmentIdentification;
import org.skarby.ja4cli.internal.ArgumentSegmentType;
import org.skarby.ja4cli.internal.ClassReporting;

public class ArgumentsAnnotationParser extends ArgumentsParser {

	public ArgumentsAnnotationParser(Environment environment) {
		super(environment);
	}

	@Override
	protected <C> ParseResult<C> parse(C configuration, Environment environment, String[] arguments) throws ConfigurationParseException {
		ParseResult<C> result = new ParseResult<C>();
		try {
			if (environment.isDebugMode()) {
				System.out.println("CONVERTERS :: ");
				System.out.println("=============================================================================");
				for (ConverterFactory<? extends ArgumentConverter<?>, ?> each : environment.getConverterFactories()) {
					System.out.println(ClassReporting.inspectClass(each.getClass()));
				}
				
				System.out.println("\nCONFIGURATION :: ");
				System.out.println("=============================================================================");
				System.out.println(ClassReporting.inspectClass(configuration.getClass()));
			}
			
			ArgumentSegmentIdentification identification = new ArgumentSegmentIdentification();
			List<Set<ArgumentSegmentType>> segmentTypes = identification.identify(arguments);
			for (Set<ArgumentSegmentType> each : segmentTypes) {
				System.out.println(each);
			}
		} catch (Exception e) {
			throw new ConfigurationParseException("Uneable to parse configuration.", e);
		}

		return result;
	}
}
